package com.wm.gridimageupload.util

import android.text.TextUtils
import com.luck.picture.lib.entity.LocalMedia
import com.wm.gridimageupload.bean.ImageInfoBean

object ImageUtils {

    fun filterImage(data: MutableList<ImageInfoBean>, selectList: MutableList<LocalMedia>, type: String) {

        val images: MutableList<ImageInfoBean> = mutableListOf()

        for (localMedia in selectList) {
            val imageInfoBean = ImageInfoBean()
            imageInfoBean.setType(type)
            val path = if (TextUtils.isEmpty(localMedia.originalPath)) localMedia.realPath else localMedia.originalPath

            var isHas = false
            for (bean in data) {
                // 图片在列表中已经存在
                if (path != null && path == bean.getPath()) {
                    isHas = true
                }
            }

            // 列表中不存在这张图
            if (!isHas) {
                imageInfoBean.setPath(path)
                imageInfoBean.setLocalMedia(localMedia)
                images.add(imageInfoBean)
            }
        }

        data.addAll(images)
    }
}